//发货单登记-费用申领
//赵永平

interface IOrderCostApplication extends ng.IScope{
    /**
     * 合同号
     */
    contractNumber:string;
    /**
     * 承运商名称
     */
    carrierName:string;
    /**
     * 申领人
     */
    proposer:string;
    /**
     * 申领密码
     */
    applyPassword:string;
    /**
     * 申领密码错误提示
     */
    applyPasswordDis:boolean;
    /**
     * 开户银行启用禁用
     */
    openBankDis:boolean;
    /**
     * 费用类型枚举
     */
    feeTypeDropDown:ValueListItemResponse[];
    /**
     * 付款方式
     */
    paymentMethodList:ValueListItemResponse[];
    /**
     * 新增费用
     */
    addFeeSave:() => void;
    /**
     * 编辑费用(获取费用详情)
     */
    getFeeInfo:(data:feeListResponse) =>void;
    /**
     * 编辑保存费用数据
     */
    editFeeSave:(data:feeListResponse) => void;

    /**
     * 删除费用
     */
    deleteFeeSave:(id:string) => void;

    /**
     * 添加费用申领model
     */
    addFeeData:feeDataInfo|any;
    /**
     * 编辑费用申领model
     */
    editFeeData:feeDataInfo|any;
    /**
     * 提交申领密码事件
     */
    verifyPW:(id?:string) => void;
    /**
     * 保存费用
     */
    saveFee():void;
    /**
     * 费用列表
     */
    feeList:Array<feeListResponse>;
    /**
     * 申领密码input显示隐藏
     */
    applyMoneyDis:boolean;
    /**
     * 银行卡信息disable启、禁
     */
    feeBankDis:boolean;
    /**
     * 编辑费用disable 启、禁
     */
    feeDis:boolean;  
    /**
     * 获取银行卡信息
     */
    getBankNameInfo():void;
    /**
     * 编辑页面确认申领金额
     */
    confirmApplyFeeBox:number;
    /**
     * 银行卡自动补全
     */
    onBankAutoComplate:(value: string) => any;
    /**
     * 银行卡选中填充
     */
    onBankSelected:(select:angucomplete<BankListItemResponse>)=>any;
    /**
     * 返回
     */
    skipBack():void;
    /**
     * 费用申领防止并发
     */
    verifyPWDis:boolean;
}

interface feeDataInfo {
    /**
     * 费用编号
     */
    id?:string;
    /**
     * 承运商单编号
     */
    carrierOrderId:string,
    /**
     * 费用类型
     */
    feeType:string,
    /**
     * 申请金额
     */
    applyFee:number,
    /**
     * 支出原因
     */
    payReson:string,
    /**
     * 承运商
     */
    carrier:string,
    /**
     * 承运商编号
     */
    carrierId:string,
    /**
     * 合同编号
     */
    contractId:string,
    /**
     * 合同短号
     */
    contractNumber:string,
    /**
     * 费用支付方式
     */
    paymentMethod:string,
    /**
     * 申领人
     */
    applicant:string;
    /**
     * 验证申领密码返回token值
     */
    applyToken:string;
    /**
     * 持卡人
     */
    holder:string;
    /**
     * 银行卡号
     */
    bankCode:string;
    /**
     * 开户银行
     */
    openBank:string;

    /**
     * 申领密码
     */
    applyPassword:string;
    
}

class orderCostAppilcationController {
    /**
     * 承运单Id
     */
    carrierOrderId:string;
    /**
     * 承运商id
     */
    carrierId:string="";
    constructor(private $scope:IOrderCostApplication,private orderService:IorderService,private valueService:IValueService,private $location:ng.ILocationService,private bankService:IBankService,private userService:IUserService,private $stateParams:any,private $ngBootbox:BootboxService,private $state:ng.ui.IStateService,private commonService:ICommonService){
        this.init();
    }
    init():void{
        this.carrierOrderId = this.$location.search().id;
        this.$scope.applyPasswordDis = false;
        this.$scope.openBankDis = true;
        // this.$scope.addFeeData.carrierOrderId = this.carrierId;
        this.$scope.applyMoneyDis = true; 
        this.$scope.feeBankDis = true;
        this.$scope.feeDis = true;
        //获取枚举数据
        this.loadValueDropDown();
        //获取承运商信息
        this.getCarryOrderInfo();
        this.$scope.proposer = JSON.parse(window.localStorage.getItem('loginData')).realName; //申领人获取登录人名称(获取参数未定)
        
        //添加费用申领
        this.$scope.addFeeSave = this.addFeeSave;
        //获取费用列表
        this.getFeeList()
        //编辑费用列表
        this.$scope.editFeeSave = this.editFeeSave;
        //获取银行卡信息
        this.$scope.getBankNameInfo = this.commonService.debounce(this.getBankNameInfo,500);
        this.$scope.onBankAutoComplate = this.onBankAutoComplate;
        this.$scope.onBankSelected = this.onBankSelected;

        //删除费用
        this.$scope.deleteFeeSave = this.deleteFeeSave;

        this.$scope.addFeeData = {};
        this.$scope.editFeeData = {};
        this.$scope.editFeeData.openBankDis = true;
        
        this.$scope.skipBack = this.skipBack;

    }
    /**
     * 枚举数据初始化
     */
    loadValueDropDown():void{
         let valueData = this.valueService;
         this.$scope.feeTypeDropDown = valueData.getFeeTypeList().data;
         this.$scope.paymentMethodList = valueData.getPaymentMethodList().data;
    };

    
    isAny(response: string | {}): response is {} {
        return (<{}[]>response) !== undefined;
    }
    /**
     * 获取承运商信息
     */
    getCarryOrderInfo=()=>{
        this.orderService.carryOrder(this.carrierOrderId).then(res=>{
            this.$scope.carrierName = res.data.carrierName;
            this.$scope.contractNumber = res.data.contract.contractNumber;
            this.$scope.addFeeData.holder = res.data.holder;
            this.$scope.addFeeData.bankCode = res.data.bankCode;
            this.$scope.addFeeData.openBank = res.data.openBank;
            // this.$scope.editFeeData.carrierId = res.data.carrierId;
            this.carrierId = res.data.carrierId;
            this.$scope.editFeeData.contractId = res.data.contract.contractId;
        });
    };

    /**
     * 添加费用
     */
    addFeeSave=()=>{
        if (!$("#costForm").valid()) { return };
        if(!this.$scope.feeList.every(this.isQqual)){
            this.$ngBootbox.confirm("该合同已申领过相同类型的费用,确认新增吗?").then(()=>{
                this.$scope.applyPassword = "";
                 $("#passWord").modal("show");//验证密码弹窗
                this.$scope.openBankDis = true;
                this.$scope.applyPasswordDis = false;
                //验证申领密码
                this.$scope.verifyPW = this.verifyPW;
            });
        }else{
            this.$scope.applyPassword = "";
            $("#passWord").modal("show");//验证密码弹窗
            this.$scope.openBankDis = true;
            this.$scope.applyPasswordDis = false;
            //验证申领密码
            this.$scope.verifyPW = this.verifyPW;
        }
    }

    //判断新增费用类型是否已有相同类型
    isQqual=(item:feeListResponse,index:number,array:any)=>{
        if (String(item.feeType) === this.$scope.addFeeData.feeType) {
            return false
        } else {
            return true
        }
    }


    /**
     * 验证申领密码并且添加费用申领
     */
    verifyPW = () => {
        this.$scope.verifyPWDis = true;
        let applyPasswordMD5 = md5(this.$scope.applyPassword);//md5
        // let applyPasswordMD5 = this.$scope.applyPassword;
        this.userService.checkApplyPassword(applyPasswordMD5).then(res=>{
            this.$scope.applyPasswordDis = false;
            //判断申领密码正确
            if(res.data.isRightPassword){
                this.orderService.addFee(
                    this.carrierOrderId,
                    this.$scope.addFeeData.feeType,
                    this.$scope.addFeeData.applyFee,
                    this.$scope.addFeeData.confirmApplyFee,
                    this.$scope.addFeeData.payReson,
                    this.$scope.carrierName,
                    this.carrierId,
                    this.$scope.editFeeData.contractId,
                    this.$scope.contractNumber,
                    this.$scope.addFeeData.paymentMethod,
                    this.$scope.addFeeData.applicant,
                    res.data.applyToken,
                    this.$scope.addFeeData.holder,
                    this.$scope.addFeeData.bankCode,
                    this.$scope.addFeeData.openBank
                ).then(res=>{
                    $('#detail').modal('hide');//关闭弹窗
                    this.getFeeList();
                     this.$scope.verifyPWDis = false;
                    $("#passWord").modal("hide");//验证申领密码
                },rej=>{ this.$scope.verifyPWDis = false;$('#detail').modal('hide');$("#passWord").modal("hide");});
            }else{
                this.$scope.applyPasswordDis = true;
                this.$scope.verifyPWDis = false;
            }
        });
    };

    /**
     * 编辑保存费用
     */
    editFeeSave = (data:feeListResponse) => {
        this.$scope.applyPassword = "";
        this.$scope.confirmApplyFeeBox = null;
        this.$scope.applyPasswordDis = false;
        this.$scope.openBankDis = true;
        
        //判断费用状态
        if(data.feeStatusStr =="退回办理"){
            this.$scope.applyMoneyDis = true;
            this.$scope.feeBankDis = false;
            this.$scope.feeDis = false;
        }else if(data.feeStatusStr =="办理"){
            this.$scope.applyMoneyDis = true;
            this.$scope.feeBankDis = true;
            this.$scope.feeDis = false;
        }else{
            this.$scope.applyMoneyDis = false;
            this.$scope.feeBankDis = true;
            this.$scope.feeDis = true;
        }
        //赋值
        this.$scope.editFeeData.id = data.feeId;
        this.$scope.editFeeData.feeType = String(data.feeType);
        this.$scope.editFeeData.applyFee = data.applyAmount;
        this.$scope.editFeeData.payReson = data.payReason;
        this.$scope.editFeeData.carrier = data.settleCarrier;
        this.$scope.editFeeData.paymentMethod = String(data.paymentMethod);
        this.$scope.editFeeData.applicant = data.applyPerson;
        this.$scope.editFeeData.holder = data.holder;
        this.$scope.editFeeData.bankCode = data.bankCode;
        $("#bankCode_value").val(data.bankCode);
        this.$scope.editFeeData.openBank = data.openBank;

        //编辑保存费用
        this.$scope.saveFee = ()=>{
             $("#passWord").modal("show");//验证密码弹窗
             this.$scope.applyPassword  = "";
             this.$scope.verifyPW = ()=>{
                 this.EditVerifyPW(data.feeId)
             }
        };
    }
    /**
     * 编辑保存费用
     */
    EditVerifyPW = (id:string) => {
        this.$scope.verifyPWDis = true;
        let applyPasswordMD5 = md5(this.$scope.applyPassword);//md5
        // let applyPasswordMD5 = this.$scope.applyPassword;
        this.userService.checkApplyPassword(applyPasswordMD5).then(res=>{
            //判断申领密码正确
            if(res.data.isRightPassword){
                this.$scope.applyPasswordDis = false;
                this.orderService.editFee(
                    id,
                    this.carrierOrderId,
                    this.$scope.editFeeData.feeType,
                    this.$scope.editFeeData.applyFee,
                    Number(this.$scope.confirmApplyFeeBox),
                    this.$scope.editFeeData.payReson,
                    this.$scope.carrierName,
                    this.carrierId,
                    this.$scope.editFeeData.contractId,
                    this.$scope.contractNumber,
                    this.$scope.editFeeData.paymentMethod,
                    this.$scope.editFeeData.applicant,
                    res.data.applyToken,
                    this.$scope.editFeeData.holder,
                    $("#bankCode_value").val(),
                    this.$scope.editFeeData.openBank
                ).then(res=>{
                    $('#detail').modal('hide');//关闭弹窗
                    this.getFeeList();
                    this.$scope.verifyPWDis = false;
                    $("#passWord").modal("hide");//验证申领密码
                },rej=>{ this.$scope.verifyPWDis = false;});
            }else{
                this.$scope.verifyPWDis = false;
                this.$scope.applyPasswordDis = true;
            }
        });
    }


    /**
     * 获取申请费用列表
     */
    getFeeList():void{
        this.orderService.feeList(this.carrierOrderId).then(res=>{
            this.$scope.feeList = res.data.data;
        })
    };

    /**
     * 获取银行卡信息
     */
    getBankNameInfo=()=>{
        let banKLength = $("#bankCode_value").val().length;
        if(banKLength > 15){
            this.bankService.getBankName($("#bankCode_value").val()).then(res=>{
                if(!res){this.$scope.openBankDis = false}{
                    this.$scope.editFeeData.openBank = res.data;
                    this.$scope.openBankDis = true;
                }
            },ref=>{this.$scope.editFeeData.openBank = ""; this.$scope.openBankDis = false;});
        }
    }
    /**
     * 银行卡自动加载填充数据
     */
    onBankAutoComplate=(value:string)=>{
        if(this.carrierId === ""){return}
        return this.bankService.getList("",this.carrierId,value,false,0,5).then(res => {
            return res.data.data;
        })
    }
    /**
     * 银行卡选中填充数据
     */
    onBankSelected=(selected:angucomplete<BankListItemResponse>)=>{
        if(selected){
            this.$scope.editFeeData.holder = selected.description.holder;
            this.$scope.editFeeData.openBank= selected.description.bank;
            this.$scope.editFeeData.openBankDis = true;
        }
    }
    /**
     * 删除费用
     */
    deleteFeeSave = (feeId:string) => {
        this.$ngBootbox.confirm("确定删除费用吗？").then(()=>{
            this.orderService.deleteFee(feeId,this.carrierOrderId).then(res=>{
                this.getFeeList();
            });
        });
    }
    skipBack():void{
        // window.history.back();
        this.$state.go("app.order.orderRegisterList",{name:"orderRegisterList"});
    }

}
angular.module("tms").controller("orderCostAppilcationController",["$scope","orderService","valueService","$location","bankService","userService","$stateParams","$ngBootbox","$state","commonService",orderCostAppilcationController])